<?php


namespace app\home\controller;

use app\home\model\Jibing as JibingModel;

use think\Controller;
use think\Request;

class Jibing extends Controller
{
    protected $jibingModel;

    public function __construct(
        JibingModel $jibingModel
    )
    {
        parent::__construct();
        $this->jibingModel = $jibingModel;
    }

    public function show(Request $request)
    {
        return $this->fetch();
    }

    public function lists()
    {
        return $this->fetch();
    }

    public function ajax_lists(Request $request)
    {
        $page = $request->param('page', 1);
        $limit = $request->param('limit', 10);
        $where = [];
        $data = $this->jibingModel->where($where)->page($page, $limit)->select();
        $count = $this->jibingModel->where($where)->count();
        return reorganizeTableData(0, '', $count, $data);
    }

    public function info(Request $request)
    {
        $pid = $request->param('pid', '');
        $res = $this->jibingModel->where('pid', $pid)->find();
        if(isset($res['zhiliao_text']) && !empty($res['zhiliao_text'])){
            $res['zhiliao_text'] = json_decode($res['zhiliao_text'],1);
        }
        if(isset($res['zhiliao_state_text']) && !empty($res['zhiliao_state_text'])){
            $res['zhiliao_state_text'] = json_decode($res['zhiliao_state_text'],1);
        }
        if(isset($res['ruyuanmudi_text']) && !empty($res['ruyuanmudi_text'])){
            $res['ruyuanmudi_text'] = json_decode($res['ruyuanmudi_text'],1);
        }
        return jsonRes($res);
    }

    public function update(Request $request)
    {
        $pid = $request->param('pid', '');
        if(!isset($pid) || empty($pid)){
            return jsonRes([],'pid不能为空',99);
        }
        $zddate = $request->param('zddate', '');
        $jutiriqibujide = $request->param('jutiriqibujide', '0');
        $zhongliu_list = $request->param('zhongliu_list', '');
        $bl_yingxiangxue = $request->param('bl_yingxiangxue', '');
        $bl_binglifenqi_1 = $request->param('bl_binglifenqi_1', '');
        $bl_binglizhenduan_1 = $request->param('bl_binglizhenduan_1', '');
        $p_t_1 = $request->param('p_t_1', '');
        $p_n_1 = $request->param('p_n_1', '');
        $p_m_1 = $request->param('p_m_1', '');
        $p_qibie_1 = $request->param('p_qibie_1', '');
        $p_info_1 = $request->param('p_info_1', '');
        $c_t_1 = $request->param('c_t_1', '');
        $c_n_1 = $request->param('c_n_1', '');
        $c_m_1 = $request->param('c_m_1', '');
        $c_qibie_1 = $request->param('c_qibie_1', '');
        $c_info_1 = $request->param('c_info_1', '');
        $bl_zhongliu_fenhua_1 = $request->param('bl_zhongliu_fenhua_1', '');
        $bl_binglifenqi_dc = $request->param('bl_binglifenqi_dc', '');
        $bl_binglizhenduan_dc = $request->param('bl_binglizhenduan_dc', '');
        $p_t_dc = $request->param('p_t_dc', '');
        $p_n_dc = $request->param('p_n_dc', '');
        $p_m_dc = $request->param('p_m_dc', '');
        $p_qibie_dc = $request->param('p_qibie_dc', '');
        $p_info_dc = $request->param('p_info_dc', '');
        $c_t_dc = $request->param('c_t_dc', '');
        $c_n_dc = $request->param('c_n_dc', '');
        $c_m_dc = $request->param('c_m_dc', '');
        $c_qibie_dc = $request->param('c_qibie_dc', '');
        $c_info_dc = $request->param('c_info_dc', '');
        $bl_zhongliu_fenhua_dc = $request->param('bl_zhongliu_fenhua_dc', '');
        $heliu = $request->param('heliu', '');
        $zhiliao = $request->param('zhiliao', '');
        $zhiliao_state = $request->param('zhiliao_state', '');
        $feibie = $request->param('feibie', '');
        $yingyang = $request->param('yingyang', '');
        $pntype = $request->param('pntype', '');
        $pnzhiji = $request->param('pnzhiji', '');
        $entype = $request->param('entype', '');
        $enzhiji = $request->param('enzhiji', '');
        $yingyangliang = $request->param('yingyangliang', '');
        $hdoptions = $request->param('hdoptions', '');
        $jsoptions = $request->param('jsoptions', '');
        $ruyuanmudi = $request->param('ruyuanmudi', '');
        $zhiliao_text = $request->param('zhiliao_text', '');
        $zhiliao_state_text = $request->param('zhiliao_state_text', '');
        $ruyuanmudi_text = $request->param('ruyuanmudi_text', '');

        $data = [
            'pid' => $pid,
            'zddate' => $zddate,
            'jutiriqibujide' => $jutiriqibujide,
            'zhongliu_list' => $zhongliu_list,
            'bl_yingxiangxue' => $bl_yingxiangxue,
            'bl_binglifenqi_1' => $bl_binglifenqi_1,
            'bl_binglizhenduan_1' => $bl_binglizhenduan_1,
            'p_t_1' => $p_t_1,
            'p_n_1' => $p_n_1,
            'p_m_1' => $p_m_1,
            'p_qibie_1' => $p_qibie_1,
            'p_info_1' => $p_info_1,
            'c_t_1' => $c_t_1,
            'c_n_1' => $c_n_1,
            'c_m_1' => $c_m_1,
            'c_qibie_1' => $c_qibie_1,
            'c_info_1' => $c_info_1,
            'bl_zhongliu_fenhua_1' => $bl_zhongliu_fenhua_1,
            'bl_binglifenqi_dc' => $bl_binglifenqi_dc,
            'bl_binglizhenduan_dc' => $bl_binglizhenduan_dc,
            'p_t_dc' => $p_t_dc,
            'p_n_dc' => $p_n_dc,
            'p_m_dc' => $p_m_dc,
            'p_qibie_dc' => $p_qibie_dc,
            'p_info_dc' => $p_info_dc,
            'c_t_dc' => $c_t_dc,
            'c_n_dc' => $c_n_dc,
            'c_m_dc' => $c_m_dc,
            'c_qibie_dc' => $c_qibie_dc,
            'c_info_dc' => $c_info_dc,
            'bl_zhongliu_fenhua_dc' => $bl_zhongliu_fenhua_dc,
            'heliu' => $heliu,
            'zhiliao' => $zhiliao,
            'zhiliao_state' => $zhiliao_state,
            'feibie' => $feibie,
            'yingyang' => $yingyang,
            'pntype' => $pntype,
            'pnzhiji' => $pnzhiji,
            'entype' => $entype,
            'enzhiji' => $enzhiji,
            'yingyangliang' => $yingyangliang,
            'hdoptions' => $hdoptions,
            'jsoptions' => $jsoptions,
            'ruyuanmudi' => $ruyuanmudi,
            'zhiliao_text' => json_encode($zhiliao_text,1),
            'zhiliao_state_text' => json_encode($zhiliao_state_text,1),
            'ruyuanmudi_text' => json_encode($ruyuanmudi_text,1)
        ];
        //判断数据是否存在
        $info = $this->jibingModel->where('pid', $pid)->with(['huanzhe' => function ($query) {
            $query->field('id,hospital_id,keshi,name,gender,birthday,created');
        }])->findOrEmpty()->toArray();
        if(empty($info)){
            $res = $this->jibingModel->save($data);
        }else{
            $res = $this->jibingModel->save($data,['pid' => $info['pid']]);
        }

        if ($res) {
            $resBack = jsonRes($res);
        } else {
            $resBack = jsonRes($res, '更新失败', 99);
        }
        return $resBack;
    }
}